const { KEYS } = require("./z_enter.js");
const Redis = require("@/utils/redis.js");
const BlogConfigModel = require("@/model/BlogConfig.js");
const z_base_dao = require("@/dao/z_base.js");
const { ErrorCode } = require("@/utils/r/code.js");
const ArticleModal = require("@/model/Article.js");
const UserInfoModal = require("@/model/UserInfo.js");
const MessageModal = require("@/model/Message.js");
const CategoryModal = require("@/model/Category.js");
const TagModal = require("@/model/tag.js");
const PageModal = require("@/model/Page.js");

const blog_info = {
  async GetBlogConfig() {
    //先从redis中获取信息
    let blogConfig = await Redis.GetVal(KEYS.KEY_BLOG_CONFIG);
    if (!blogConfig) {
      //如果redis中没有就查询数据库并将结果保持至redis
      blogConfig = await z_base_dao.GetOne(BlogConfigModel.BlogConfig, {
        id: 1,
      });
      blogConfig = blogConfig.config;
      //注意 这里是将具体的config信息保持到redis中的
      await Redis.Set(KEYS.KEY_BLOG_CONFIG, blogConfig, 0);
    }

    let resVO =
      typeof blogConfig === "string" ? JSON.parse(blogConfig) : blogConfig; //数据库中存储的就是json格式，set又进行了一次json处理，因此这里处理两次

    if (typeof resVO === "string") resVO = JSON.parse(resVO);
    return resVO;
  },
  async GetHomeInfo() {
    try {
      const query1 = global.mysqlPool.query(
        `SELECT COUNT(*) as articleCount FROM article WHERE status = ? AND is_delete = ?`,
        [1, 0]
      );

      const query2 = global.mysqlPool.query(
        `SELECT COUNT(*) as userCount FROM user_info`
      );

      const query3 = global.mysqlPool.query(
        `SELECT COUNT(*) as messageCount FROM message`
      );
      const [result1, result2, result3] = await Promise.all([
        query1,
        query2,
        query3,
      ]);

      const [[{ articleCount }]] = result1;
      const [[{ userCount }]] = result2;
      const [[{ messageCount }]] = result3;

      const viewCount = await Redis.GetInt(KEYS.KEY_VIEW_COUNT);
      return {
        articleCount,
        userCount,
        messageCount,
        viewCount,
      };
    } catch (error) {
      console.log(error);
    }
  },
  async UpdateBlogConfig(data) {
    data = JSON.stringify(data);

    await z_base_dao.Update(
      BlogConfigModel.BlogConfig,
      { config: data },
      { id: 1 }
    );

    await Redis.Del(KEYS.KEY_BLOG_CONFIG); //请除旧redis值
    return ErrorCode.OK;
  },
  async GetAbout() {
    //关于我界面数据只存放在redis中
    let result = await Redis.GetVal(KEYS.KEY_ABOUT);
    if (!result) result = "";
    return result;
  },
  async UpdateAbout(data) {
    await Redis.Set(KEYS.KEY_ABOUT, data.content, 0);
    return ErrorCode.OK;
  },
  async GetFrontHomeInfo() {
    const articleCount = await z_base_dao.Count2(ArticleModal.Article, {
      status: 1,
      is_delete: 0,
    });
    const userCount = await z_base_dao.Count2(UserInfoModal.UserInfo);
    const messageCount = await z_base_dao.Count2(MessageModal.Message);
    const categoryCount = await z_base_dao.Count2(CategoryModal.CategoryVo);
    const tagCount = await z_base_dao.Count2(TagModal.TagVO);
    const blogConfigVO = await this.GetBlogConfig();
    const viewCount = await Redis.GetInt(KEYS.KEY_VIEW_COUNT);
    const pageList = await z_base_dao.List(PageModal.Page);

    return {
      article_count: articleCount,
      user_count: userCount,
      message_count: messageCount,
      category_count: categoryCount,
      view_count: viewCount,
      tag_count: tagCount,
      blog_config: blogConfigVO,
      page_list: pageList,
    };
  },
};

module.exports = blog_info;
